//
//  CommentView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/4.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CommentView.h"

#import "LXTextView.h"



@interface CommentView()<UITextViewDelegate>


@property(nonatomic,strong)UIImageView * productImageView;

@property(nonatomic,strong)UILabel * commentLabel;

@property(nonatomic,strong)NSMutableArray * starButtons;

@property(nonatomic,strong)LXTextView * textView;

@property(nonatomic,strong)UIButton * anonymousButton;




@property(nonatomic,strong)UIView * line1;
@property(nonatomic,strong)UIView * line2;
@end;
@implementation CommentView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        self.starButtons = [NSMutableArray array];
        for (int i = 0; i<5; i++) {
            UIButton * star = [self starButtonWithTag:i];
            star.selected = YES;
            [self.starButtons addObject:star];
            [self addSubview:star];
        };
        self.productImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Comment_width(80), Comment_width(80))];
        self.productImageView.image = [UIImage imageNamed:@"localDetailIMage_1"];
        [self addSubview:self.productImageView];
        
        self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        self.commentLabel.text = @"评价";
        self.commentLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.commentLabel];
        
        self.textView = [[LXTextView alloc] initWithFrame:CGRectZero];
        self.textView.font = [UIFont systemFontOfSize:11];
        self.textView.placeholder = @"写下购买体会和感受帮助其他小伙伴吧~";
        [self addSubview:self.textView];
        
        
        
        
        self.line1 = [[UIView alloc] initWithFrame:CGRectZero];
        self.line1.height = 0.5;
        self.line1.backgroundColor = HexColor(@"#EEEEEE");
        [self addSubview:self.line1];
        
        self.line2 = [[UIView alloc] initWithFrame:CGRectZero];
        self.line2.height = 0.5;
        self.line2.backgroundColor = HexColor(@"#EEEEEE");
        [self addSubview:self.line2];
        
        self.anonymousButton = [[UIButton alloc] initWithFrame:CGRectZero];
        [self.anonymousButton setTitle:@"匿名评价" forState:UIControlStateNormal];
        [self.anonymousButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.anonymousButton.adjustsImageWhenHighlighted = NO;
        self.anonymousButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.anonymousButton addTarget:self action:@selector(anonymousButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.anonymousButton setImage:[UIImage imageNamed:@"selected_2_default"] forState:UIControlStateNormal];
        [self.anonymousButton setImage:[UIImage imageNamed:@"selected_2"] forState:UIControlStateSelected];
        [self.anonymousButton setImage:[UIImage imageNamed:@"selected_2"] forState:UIControlStateHighlighted];
        [self.anonymousButton setImage:[UIImage imageNamed:@"selected_2"] forState:UIControlStateSelected|UIControlStateHighlighted];

        [self addSubview:self.anonymousButton];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self addRadius:10];
    
    self.productImageView.x = globalMargin;
    self.productImageView.y = globalMargin;
    [self.starButtons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * btn = obj;
        btn.x = self.productImageView.right+(btn.width+5)*idx+globalMargin;
        btn.centerY = self.productImageView.centerY;
        
    }];
    
    self.commentLabel.x = self.productImageView.right+globalMargin;
    self.commentLabel.y = self.productImageView.y;
    self.commentLabel.width = self.width - self.productImageView.right - 5;
    self.commentLabel.height = [self.commentLabel.text sizeInAre:MaxSize font:self.commentLabel.font].height;
    
    self.line1.width = self.width - 2*globalMargin;
    self.line1.y = self.productImageView.botton + globalMargin;
    self.line1.centerX = self.width*0.5;
    
    self.textView.x = globalMargin;
    self.textView.y = self.line1.botton+5;
    self.textView.size = CGSizeMake(self.width-2*globalMargin, self.height - self.productImageView.botton -Comment_height(45)-2*globalMargin);
    
    self.line2.width = self.width - 2*globalMargin;
    self.line2.y = self.textView.botton ;
    self.line2.centerX = self.width*0.5;
    
    self.anonymousButton.height = Comment_height(15);
    self.anonymousButton.x = globalMargin;
    self.anonymousButton.width = [self.anonymousButton.currentTitle sizeInAre:MaxSize font:self.anonymousButton.titleLabel.font].width + Comment_height(15);
    self.anonymousButton.centerY = (self.height-self.line2.botton)*0.5+self.line2.botton;
}

-(void)starButtonClick:(UIButton*)button{
    button.selected = YES;
    for (int i =0; i<self.starButtons.count; i++) {
        UIButton * btn = self.starButtons[i];
        if(i<=button.tag){
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
}

-(void)anonymousButtonClick:(UIButton*)button{
    button.selected = !button.selected;
}

-(UIButton*)starButtonWithTag:(NSInteger)tag{
    UIButton * star = [UIButton buttonWithType:UIButtonTypeCustom];
    star.size = CGSizeMake(Comment_width(16), Comment_width(16));
    star.tag = tag;
    star.adjustsImageWhenHighlighted = NO;
    
    
    [star addTarget:self action:@selector(starButtonClick:) forControlEvents:UIControlEventTouchDown];
    
    [star setImage:[UIImage imageNamed:@"stars_defaul"] forState:UIControlStateNormal];
    [star setImage:[UIImage imageNamed:@"stars_selected"] forState:UIControlStateSelected];
    [star setImage:[UIImage imageNamed:@"stars_selected"] forState:UIControlStateHighlighted];
    [star setImage:[UIImage imageNamed:@"stars_selected"] forState:UIControlStateSelected | UIControlStateHighlighted];
    
    
//    [star setBackgroundImage:[UIImage imageNamed:@"stars_defaul"] forState:UIControlStateNormal];
//    [star setBackgroundImage:[UIImage imageNamed:@"stars_selected"] forState:UIControlStateSelected];
    return star;
}
@end
